package com.ambulance.project.biz.controller.uni;


import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.project.biz.entity.GatewayLog;
import com.ambulance.project.biz.service.GatewayLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDateTime;

/**
 * <p>
 * 人员信息 前端控制器
 * </p>
 *
 * @author zr
 * @since 2020-04-21
 */

@RestController
@RequestMapping(value = "/uni/v1/gateway")

public class UniGateWayController {


    private static Logger logger = LoggerFactory.getLogger(UniGateWayController.class);

    @Autowired
    private GatewayLogService gatewayLogService;

    /**
     * 接收联通推送报文
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/info")
    @ResponseBody
    public Object info(@RequestHeader(value = "token", required = false) String token,
                       HttpServletRequest request
    ) {
        // 获取HTTP请求的输入流
        // 已HTTP请求输入流建立一个BufferedReader对象
        BufferedReader br = null;
        String callbackMessage = null;
        try {
            br = new BufferedReader(new InputStreamReader(
                    request.getInputStream(), "UTF-8"));
            String buffer = null;
            // 存放请求内容
            StringBuffer xml = new StringBuffer();
            while ((buffer = br.readLine()) != null) {
                // 在页面中显示读取到的请求参数
                xml.append(buffer);
            }
            callbackMessage = xml.toString();

        } catch (IOException e) {
            e.printStackTrace();
        }
        AjaxResult success = AjaxResult.success();

        GatewayLog gatewayLog = new GatewayLog();
        gatewayLog.setReqUrl(request.getRequestURI());
        gatewayLog.setContent(callbackMessage);
        gatewayLog.setReqTime(LocalDateTime.now());
        gatewayLog.setResponse(success.toString());
        gatewayLog.setRespTime(LocalDateTime.now());
        gatewayLogService.save(gatewayLog);
        return success;
    }
}
